package com.yc.bos.service.impl;

import com.yc.bos.dao.IStaffDao;
import com.yc.bos.model.PageBean;
import com.yc.bos.model.Staff;
import com.yc.bos.service.IStaffService;
import com.yc.bos.service.base.BaseServiceImpl;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
@Transactional
public class StaffServiceImpl extends BaseServiceImpl<Staff> implements IStaffService{
    @Autowired
    private IStaffDao staffDao;
    @Override
    public void save(Staff entity) {
        staffDao.save(entity);
    }

    @Override
    public void update(Staff entity) {
        staffDao.update(entity);
/*        //1.根据id从数据库获取数据【持久状态-session有缓存，有id】
        Staff staff = staffDao.find(entity.getId());

        //2.更新数据库的数据
        staff.setName(entity.getName());
        staff.setTelephone(entity.getTelephone());
        staff.setStation(entity.getStation());
        staff.setHaspda(entity.getHaspda());
        staff.setStandard(entity.getStandard());
        System.out.println("数据库：" + staff);*/
    }

    @Override
    public void delete(Staff entity) {
        staffDao.delete(entity);
    }

    @Override
    public Staff findById(Serializable id) {
        return staffDao.findById(id);
    }

    @Override
    public List<Staff> findAll() {
        return staffDao.findAll();
    }

    @Override
    public void pageQuery(PageBean<Staff> pd) {
        staffDao.pageQuery(pd);
    }

    @Override
    public void deleteBatch(String ids) {
        String hql="update Staff set deltag =? where id=?";
        String[] idsArr=ids.split(",");
        for (String id:idsArr){
            staffDao.executeUpdate(hql,"1",id);
        }

    }

    @Override
    public List<Staff> finaAllWithNoDelete() {
        DetachedCriteria dc=DetachedCriteria.forClass(Staff.class);
        dc.add(Restrictions.eq("deltag","0"));
        return staffDao.findAllByDetachedCriteria(dc);
    }
}
